//
//  JFBaseNavigationController.m
//  JFBase
//
//  Created by Alan Wang on 2018/11/29.
//  Copyright © 2018 Alan Wang. All rights reserved.
//

#import "JFBaseNavigationController.h"
#import <QuartzCore/QuartzCore.h>
#import "JFImageHelper.h"
#import "JFMacro.h"

@interface JFBaseNavigationController ()
@property(nonatomic,strong) NSMutableArray * screenShotsImageList;
@property(nonatomic,assign) BOOL isMoving;
@property(nonatomic,strong) UIView  * backgroundView;
@end

@implementation JFBaseNavigationController{

    CGPoint _startPoint;
    UIImageView *_lastScreenShotImageView;
    UIView *_blackMask;

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
       
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    
//     _screenShotsImageList = [[NSMutableArray alloc]initWithCapacity:2];
//    UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self
//                                                                                    action:@selector(paningGestureReceive:)];
//
//
//
//
//    recognizer.delegate = self;
//    [recognizer delaysTouchesBegan];
//    [self.view addGestureRecognizer:recognizer];

}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    @try
    {
//      UIImage *image = [JFImageHelper imageByCurrentScreen];
//      if (!image)
//       {
//          image = [[UIImage alloc]init];
//       }
//       [_screenShotsImageList addObject:image];
       [super pushViewController:viewController animated:animated];
    }
    @catch (NSException *exception)
    {
        NSLog(@"%@",exception);
    }
}
// [self.navigationController popToViewController:controller animated:YES];
-(void)popToViewController:(UIViewController*)vc animated:(BOOL)animated{

       [super popToViewController:vc animated:animated];
    
}

- (void)popViewController
{
     [super popViewControllerAnimated:YES];
    
}

#pragma mark--辅助方法
- (void)resetViewX:(float)x
{
    x = x > AWSCREEN_WIDTH ? AWSCREEN_WIDTH : x;
    x = x < 0 ? 0 : x;
    CGRect frame = self.view.frame;
    frame.origin.x = x;
    self.view.frame = frame;
    float alpha = 0.4 - (x / 800);
    _blackMask.alpha = alpha;
}

#pragma mark - Gesture Recognizer -

- (void)paningGestureReceive:(UIPanGestureRecognizer *)recoginzer
{
    // 没有上一个界面，不用返回
    if (self.viewControllers.count <= 1 || !self.isSlide)
    {
        return;
    }
    CGPoint touchPoint = [recoginzer locationInView:AWKEY_WINDOW];
    //开始滑动
    if (recoginzer.state == UIGestureRecognizerStateBegan)
    {
        _isMoving = YES;
        _startPoint = touchPoint;
        //创建截屏展示容器
        if (!_backgroundView)
        {
            CGRect frame = self.view.frame;
            _backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width , frame.size.height)];
            _backgroundView.backgroundColor = [UIColor clearColor];
            //截屏界面放在当前界面的下方，移动的时候，上一个截屏界面就可以显示出来
            [self.view.superview insertSubview:_backgroundView belowSubview:self.view];
            //遮罩层
            _blackMask = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width , frame.size.height)];
            _blackMask.backgroundColor = [UIColor blackColor];
//            [_backgroundView addSubview:_blackMask];
        }
        else
        {
            //截屏界面放在当前界面的下方，移动的时候，上一个截屏界面就可以显示出来
            if (!_backgroundView.superview)
            {
//                [self.view.superview insertSubview:_backgroundView belowSubview:self.view];
            }
        }
        //截屏界面显示，开始移动手势
        _backgroundView.hidden = NO;
        //删除上次屏幕的截图
        if (_lastScreenShotImageView)
        {
            [_lastScreenShotImageView removeFromSuperview];
        }
//        //获取上一个截屏界面
//        UIImage *lastScreenShot = [_screenShotsImageList lastObject];
//        _lastScreenShotImageView = [[UIImageView alloc]initWithImage:lastScreenShot];
//        //截屏界面放到遮罩层的下方
//        [_backgroundView insertSubview:_lastScreenShotImageView belowSubview:_blackMask];
    }
    else if (recoginzer.state == UIGestureRecognizerStateEnded)
    {
        //滑动返回动画效果
        if (touchPoint.x - _startPoint.x > 50)
        {
            [UIView animateWithDuration:0.3 animations:^{
                [self resetViewX:AWSCREEN_WIDTH];
            } completion:^(BOOL finished) {
                [super popViewControllerAnimated:NO];
                [self.screenShotsImageList removeLastObject];
                //显示返回的界面，返回的界面弹出来以后，要把坐标恢复正常
                CGRect frame = self.view.frame;
                frame.origin.x = 0;
                self.view.frame = frame;
                self.isMoving = NO;
            }];
        }
        //滑动无效，还原当前显示界面
        else
        {
            [UIView animateWithDuration:0.3 animations:^{
                [self resetViewX:0];
            } completion:^(BOOL finished) {
                self.isMoving = NO;
                //截屏界面容器隐藏
                self.backgroundView.hidden = YES;
            }];
        }
        return;
    }
    //滑动取消，还原当前显示的界面
    else if (recoginzer.state == UIGestureRecognizerStateCancelled)
    {
        [UIView animateWithDuration:0.3 animations:^{
            [self resetViewX:0];
        } completion:^(BOOL finished) {
            self.isMoving = NO;
            self.backgroundView.hidden = YES;
        }];
        return;
    }
    if (_isMoving)
    {
        [self resetViewX:touchPoint.x - _startPoint.x];
    }
}

//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
//{
//    //不能滑动或者只有一个子控制器就不监听滑动手势
//    if (!_isSlide || self.viewControllers.count <= 1)
//    {
//        return NO;
//    }
//     CGPoint point = [touch locationInView:self.view];
//    //排除表格滑动事件
//    if([[[touch.view class] description] isEqualToString:@"UITableViewCellReorderControl"])
//    {
//        return NO;
//    }
//    else if (point.x > 80 && point.x < (self.view.frame.size.width - 80))
//    {
//        return NO;
//    }
//    return YES;
//}

//-(BOOL)shouldAutorotate
//{
//    return NO;
//}
//
//-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
//{
//    return NO;
//}
//
//- (UIInterfaceOrientationMask)supportedInterfaceOrientations
//{
//    return UIInterfaceOrientationMaskPortrait;
//}

- (BOOL)shouldAutorotate {
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
